package com.xinghai.system.service.impl;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.xinghai.common.utils.DateUtils;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.system.mapper.QdUserPointsLogMapper;
import com.xinghai.system.domain.QdUserPointsLog;
import com.xinghai.system.service.IQdUserPointsLogService;

/**
 * 用户钱包日志Service业务层处理
 *
 * @author xinghai
 * @date 2025-01-26
 */
@Service
public class QdUserPointsLogServiceImpl extends ServiceImpl<QdUserPointsLogMapper, QdUserPointsLog>
        implements IQdUserPointsLogService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询用户钱包日志列表
     *
     * @param qdUserPointsLog 用户钱包日志
     * @return 用户钱包日志集合
     */
    @Override
    public List<QdUserPointsLog> list(QdUserPointsLog qdUserPointsLog) {
        QueryWrapper<QdUserPointsLog> queryWrapper = new QueryWrapper<>();
        if (qdUserPointsLog.getParams().get("beginCreateTime")!= null && !"".equals(qdUserPointsLog.getParams().get("beginCreateTime"))
        &&qdUserPointsLog.getParams().get("endCreateTime")!= null && !"".equals(qdUserPointsLog.getParams().get("endCreateTime"))){
            queryWrapper.between("create_time", qdUserPointsLog.getParams().get("beginCreateTime"),qdUserPointsLog.getParams().get("endCreateTime"));
        }
        if (qdUserPointsLog.getUserPhone()!= null  && !qdUserPointsLog.getUserPhone().isEmpty()){
            queryWrapper.like("user_phone", qdUserPointsLog.getUserPhone());
        }
        if (qdUserPointsLog.getUserId()!= null){
            queryWrapper.eq("user_id", qdUserPointsLog.getUserId());
        }
        if (qdUserPointsLog.getType()!= null  && !qdUserPointsLog.getType().isEmpty()){
            queryWrapper.eq("type", qdUserPointsLog.getType());
        }
        return this.list(queryWrapper);
    }

    /**
     * 删除用户钱包日志信息
     * @param id 需要删除的数据ID
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    public boolean delete(Long id, boolean flag) {
        if (flag){
            QdUserPointsLog qdUserPointsLog = new QdUserPointsLog();
            qdUserPointsLog.setId(id);
            qdUserPointsLog.setDelFlag(GenConstants.DELETE_FLAG_YES);
            return this.updateById(qdUserPointsLog);
        }
        return this.removeById(id);
    }

    /**
     * 批量删除用户钱包日志信息
     *
     * @param ids 需要删除的数据ID集合
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    @Transactional
    public boolean deleteBatch(Long[] ids, boolean flag) {
        if (flag){
            List<QdUserPointsLog> collect = Arrays.stream(ids).map(m -> {
                QdUserPointsLog qdUserPointsLog = new QdUserPointsLog();
                qdUserPointsLog.setId(m);
                qdUserPointsLog.setDelFlag(GenConstants.DELETE_FLAG_YES);
                return qdUserPointsLog;
            }).collect(Collectors.toList());
            return this.updateBatchById(collect);
        }
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 导入用户钱包日志信息
     *
     * @param list 用户钱包日志信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<QdUserPointsLog> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            QdUserPointsLogMapper qdUserPointsLogMapper = sqlSession.getMapper(QdUserPointsLogMapper.class);
            list.forEach(qdUserPointsLogMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
